/**
 * @Project TMHSC
 * @Author Tran Manh Hung
 * @Email: hungtmit@gmail.com | tmh@xwebgate.com
 * @Copyright © 2011 TMH. All rights reserved
 * @Createdate Jan 30, 2011
 * @Createtime 10:21:03 PM
 */

package com.tmh.study.aASA
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	import flash.geom.Matrix;
	import flash.geom.Point;

	/**
	 * Use BitmapData.hitTest for bitmaps
	 */
	public class BitmapCollision1 extends Sprite
	{
		private var bmpd1:BitmapData;

		private var bmp1:Bitmap;

		private var bmpd2:BitmapData;

		private var bmp2:Bitmap;

		public function BitmapCollision1():void
		{
			var star:Star = new Star(50);

			// make a fixed bitmap and draw star into it
			bmpd1 = new BitmapData(100, 100, true, 0);
			bmpd1.draw(star, new Matrix(1, 0, 0, 1, 50, 50));
			bmp1 = new Bitmap(bmpd1);
			bmp1.x = 200;
			bmp1.y = 200;
			addChild(bmp1);

			// make a moveable bitmap, draw star into it, too
			bmpd2 = new BitmapData(100, 100, true, 0);
			bmpd2.draw(star, new Matrix(1, 0, 0, 1, 50, 50));
			bmp2 = new Bitmap(bmpd2);
			addChild(bmp2);

			stage.addEventListener(MouseEvent.MOUSE_MOVE, _mouseMoveHandler);
		}

		private function _mouseMoveHandler(event:MouseEvent):void
		{
			// move bmp2 to mouse position (centered)
			bmp2.x = mouseX - 50;
			bmp2.y = mouseY - 50;

			/**
			 * the hit test itself
			 *
			 * alphaThreshold: 0 - 255
			 * 255 mean a pixel must be fully opaque for hit test!!!
			 */
			if (bmpd1.hitTest(new Point(bmp1.x, bmp1.y), 0xff, bmpd2, new Point(bmp2.x, bmp2.y), 0xff))
			{
				bmp1.filters = [new GlowFilter()];
				bmp2.filters = [new GlowFilter()];
			}
			else
			{
				bmp1.filters = [];
				bmp2.filters = [];
			}
		}
	}
}